﻿<?php	
	require('includes/modules/payment/glbpay/glbpay_application_top.php'); 
	require('includes/modules/payment/glbpay/glbpay_payresult.php');
	require('includes/languages/' . $_SESSION['language'] . '/modules/payment/glbpay.php');
	$m_dccorder=array();
	$merchantid=MODULE_PAYMENT_GLBPAY_MERCHANTID;
	$txnid=$_POST["hdtxnid"];
	$selectCurrency=$_POST["hdSelectCurrency"];
	$selectAmount=$_POST["hdSelectAmount"];
	
	$m_dccorder["merchantid"]=$merchantid;
	$m_dccorder["txnid"]=$txnid;
	$m_dccorder["selectCurrency"]=$selectCurrency;
	$m_dccorder["selectAmount"]=$selectAmount;
	$signStr=$merchantid.$txnid.$selectCurrency.$selectAmount;
	$m_dccorder["signature"]=md5(MODULE_PAYMENT_GLBPAY_SSLKEY.$signStr); 
	
	$payUrl='';
	if(MODULE_PAYMENT_GLBPAY_HANDLER == 'Live'){
		$payUrl='https://pgw.glbpay.com/Api/DccPaymentConfirm'; 
	}else{
		$payUrl='http://api.grapay.net:87/Api/DccPaymentConfirm';
	}
	_log('confirm dcc parameter:'.json_encode($m_dccorder),'INFO');
	$messageStack = new messageStack();	
	$result = @GetWebContent($payUrl,json_encode($m_dccorder));  
	_log('dcc confirm result :'.$result,'INFO');
	
	$payResult=new payResult();
	//处理订单状态
	$payResult->UpdateOrder($result);
	
	
	function GetWebContent($url,$data){  		
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); 		
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");     		
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1);  
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); 
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);		
	    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($data))
        );		
		$curl_response = curl_exec($curl); 
		if ($curl_response === false) {
			$info = curl_getinfo($curl);
			curl_close($curl); 
		}
		curl_close($curl);
		return $curl_response; 				                                                              
	}
	
	function _log($msg,$lavel)
	{
		error_log(date('Y-m-d H:i:s',time())." ".$lavel." - ".$msg . ";\r\n", 3, DIR_FS_SQL_CACHE."/glbpay-info-".date('Y-m-d',time()).".log");
	}
?>